package com.dmwork.service.customer.controller.front.me;


import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.customer.entity.FollowGroup;
import com.dmwork.service.customer.entity.vo.FollowGroupInfoVo;
import com.dmwork.service.customer.service.CustomerFollowerService;
import com.dmwork.service.customer.service.FollowGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-03-13
 */
@Slf4j
@Api(description = "关注分组管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/customer/follow_group/me")
public class FrontMeFollowGroupController {

    @Autowired
    private FollowGroupService followGroupService;
    @Autowired
    private CustomerFollowerService customerFollowerService;

    @ApiOperation("新建关注分组")
    @PostMapping("create_follow_group")
    public R createFollowGroup(
            HttpServletRequest request,
            @ApiParam(value = "关注分组信息", required = true) @RequestBody FollowGroup followGroup) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        followGroup.setCustomerId(jwtInfo.getId());
        followGroupService.save(followGroup);
        log.info(followGroup.toString());
        FollowGroupInfoVo followGroupInfoVo = new FollowGroupInfoVo();
        BeanUtils.copyProperties(followGroup, followGroupInfoVo);
        return R.ok().data("item", followGroupInfoVo);
    }

    @ApiOperation("获取当前用户所有关注分组")
    @GetMapping("get_group_list")
    public R getGroupList(HttpServletRequest request) {
        JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        List<FollowGroupInfoVo> followGroupInfoVoList = followGroupService.getFollowGroupList(jwtToken.getId());
        return R.ok().data("items", followGroupInfoVoList);
    }

    @ApiOperation("删除分组")
    @DeleteMapping("remove_follow_group/{followGroupId}")
    public R removeFollowGroup(
            HttpServletRequest request,
            @ApiParam(value = "分组id", required = true) @PathVariable("followGroupId") String followGroupId) {

        //删除分组，先判断是否是系统自带分组，系统自带分组不允许删除
        if (followGroupId.equals("000")||followGroupId.equals("111")) {
            return R.error().message("系统自带分组无法删除");
        }else {
            JwtInfo jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
            String followerId = jwtToken.getId(); //关注者用户id
            //取关该用户在此分组内的所有用户
            customerFollowerService.unFollowAll(followerId, followGroupId);
            followGroupService.removeById(followGroupId);
            return R.ok().message("删除成功");
        }
    }

}

